﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;

namespace AMDev.BNET.ClientAPI
{
    internal class JSONResponseDeserializer<T>
    {
        #region .ctor     
        #endregion

        #region Methods

        public T Deserialize(Stream source)
        {
            DataContractJsonSerializer jsonSerializer = null;
            T deserializedObject;                        
            
            jsonSerializer = new DataContractJsonSerializer(typeof(T));
            deserializedObject = (T)jsonSerializer.ReadObject(source);
            return deserializedObject;
        }

        public T Deserialize(String source)
        {
            Byte[] byteArray = null;
            MemoryStream objectMemoryStream = null;
            T deserializedObject;          

            byteArray = Encoding.UTF8.GetBytes(source);
            objectMemoryStream = new MemoryStream(byteArray);           
            deserializedObject = this.Deserialize(objectMemoryStream);
            objectMemoryStream.Dispose();
            return deserializedObject;
        }

        #endregion
    }
}
